<?php
namespace Home\Controller;

class CollectionController extends HomeController
{
	public function index()
	{
		// 判断有没有登录，如果没有登录不能使用，跳转到首页
		if(empty($_SESSION['home_user'])){
			$this->redirect('/home/index/index','',1,'<script>alert("你还没有登录哦(⊙o⊙)！")</script>');
		}

		// 获取用户信息
		$name = $_SESSION['home_user']['name'];
		$this->assign('username',$name);
		$userid = $_SESSION['home_user']['id'];
		// 查询用户信息
		$data=M('user')->where(array('id'=>$userid))->find();

		$this->assign('user',$data);

		$value = 'bg-hide';
		$this->assign('all',$value);
		$this->assign('payed',$value);
		$this->assign('bundles',$value);
		$this->assign('activited',$value);
		$this->assign('collect',$value);

		// 查询藏书
		$Collect = M("collect");
		// 统计藏书数量
		$count = $Collect->where("uid=$userid")->count();
		$this->assign('count',$count);
 

		$item = $_GET['item'];
		if ($item == 'collect')
		{
			$v = '';
			$status = '已收藏的';
			$this->assign('collect',$v);
			$array = $Collect->field('jsp_collect.id,jsp_book.name,jsp_book.image')->join('jsp_book on jsp_collect.bid=jsp_book.bookid')->where("uid=$userid")->order('id desc')->select();
			$this->assign('list',$array);
			$this->assign('status',$status);
		} else if ($item == 'all'){
			$v = '';
			$this->assign('all',$v);
		} else if ($item == 'payed'){
			$v = '';
			$this->assign('payed',$v);
		} else {
			$v = '';
			$this->assign('all',$v);
		}
		$this->display();
	}


	public function delajax()
	{		
		$id = I('id');
		
	    $Collect = M('collect');

	    $result = $Collect->where("id = $id")->delete();

        if($result){
           	$this->ajaxReturn($result);        
        }
        
    }
}